import { CreatePolyline } from "./createPolyline"

interface dataProps {
  allLine: any[]
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    longitude: 114.070699,//中心经度
    latitude: 22.567979,//中心纬度
    target: {
      longitude: 114.025469,//中心经度
      latitude: 22.534472,//中心纬度
    },
    markers: [{
      id: 1,
      latitude: 22.567979,
      longitude: 114.070699,
      width: 23,
      height: 33
    }, {
      id: 0,
      latitude: 22.534472,
      longitude: 114.025469,
      width: 24,
      height: 34
    }],
    allLine: <any[]>[],
    busLineList: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.onBus()
  },
  //驾车（driving）路线规划
  onDriving() {
    const _this = this
    wx.request({
      //地图WebserviceAPI驾车路线规划接口 请求路径及参数（具体使用方法请参考开发文档）
      url: `https://apis.map.qq.com/ws/direction/v1/driving/?key=VXABZ-I5TWR-TETWR-WQGYK-Y665O-ZNB5N&from=${this.data.latitude},${this.data.longitude}&to=${this.data.target.latitude},${this.data.target.longitude}`,
      success(res: any) {
        var result = res.data.result
        var route = result.routes[0]
        console.log(res.data)

        var coors = route.polyline, pl = [];
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        var kr = 1000000;
        for (var i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
        }
        //将解压后的坐标放入点串数组pl中
        for (var i = 0; i < coors.length; i += 2) {
          pl.push({ latitude: coors[i], longitude: coors[i + 1] })
        }
        _this.setData({
          // 将路线的起点设置为地图中心点
          latitude: pl[0].latitude,
          longitude: pl[0].longitude,
          // 绘制路线
          polyline: [{
            points: pl,
            color: '#0f0',
            width: 8,
            borderColor: '#2f693c',
            borderWidth: 1
          }]
        })
      }
    })
  },
  //公交（transit）路线规划
  onBus() {
    const _this = this
    wx.request({
      url: `https://apis.map.qq.com/ws/direction/v1/transit/?key=VXABZ-I5TWR-TETWR-WQGYK-Y665O-ZNB5N&from=${this.data.latitude},${this.data.longitude}&to=${this.data.target.latitude},${this.data.target.longitude}`,
      success(res: any) {
        var result = res.data.result

        console.log(res.data)
        //全部线路
        const allLine: any[] = []
        console.log('result.routes[0].steps', result.routes[0].steps)






        result.routes.forEach((line: any) => {
          const plArr: any[] = []

          const cp = new CreatePolyline()
          const liness = cp.init(line.steps)
          console.log('liness', liness)
          allLine.push(liness)
          line.steps.forEach((item: any, index: number) => {
            var route = item
            var coors: any[] = [], pl: any[] = [];

            if (route.mode === 'WALKING') {
              coors = route.polyline
            } else if (route.mode === 'TRANSIT') {
              coors = route.lines[0].polyline
            }
            //坐标解压（返回的点串坐标，通过前向差分进行压缩）
            var kr = 1000000;
            for (var i = 2; i < coors.length; i++) {
              coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
            }
            //将解压后的坐标放入点串数组pl中
            for (var i = 0; i < coors.length; i += 2) {
              pl.push({ latitude: coors[i], longitude: coors[i + 1] })
            }

            plArr.push({
              points: pl,
              color: route.mode === 'WALKING' ? '#666' : '#f20',
              width: route.mode === 'WALKING' ? 2 : 8,
              borderColor: route.mode === 'WALKING' ? '#999' : '#f40',
              borderWidth: route.mode === 'WALKING' ? 0 : 1,
              dottedLine: route.mode === 'WALKING' ? true : false
            })
          })

          console.log('plArr', plArr)
          // allLine.push(plArr)

        })
        _this.setData({
          allLine: allLine,
          busLineList: result.routes.length
        })
        console.log('allLine', allLine)
        _this.setData({
          // 将路线的起点设置为地图中心点
          latitude: (_this.data.latitude + _this.data.target.latitude) / 2,
          longitude: (_this.data.longitude + _this.data.target.longitude) / 2,
          // 绘制路线
          polyline: [..._this.data.allLine[0][0], ..._this.data.allLine[0][1]]
        })
      }
    })
  },
  onSelectBusPath(res: any) {
    this.setData({
      polyline: [...this.data.allLine[res.currentTarget.dataset.index][0]]
    })
  },
  //点击路线
  onPolylinetap(res: any) {
    console.log('点击路线', res)
    this.data.allLine[0][0].forEach((item: any) => {
      const r = item.points.findIndex((points: any) => {
        console.log('points', points)
        return points.latitude === res.detail.latitude && points.longitude === res.detail.longitude
      })
      console.log(r)
    })

  }
})